<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>mock_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_mock_test.js.html">mock_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.MockTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.MockTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.Mock&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.MockControl&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.MockExpectation&#39;);
<a name="line23"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line24"></a>
<a name="line25"></a>// The object that we will be mocking
<a name="line26"></a>var RealObject = function() {
<a name="line27"></a>};
<a name="line28"></a>
<a name="line29"></a>RealObject.prototype.a = function() {
<a name="line30"></a>  fail(&#39;real object should never be called&#39;);
<a name="line31"></a>};
<a name="line32"></a>
<a name="line33"></a>RealObject.prototype.b = function() {
<a name="line34"></a>  fail(&#39;real object should never be called&#39;);
<a name="line35"></a>};
<a name="line36"></a>
<a name="line37"></a>var matchers = goog.testing.mockmatchers;
<a name="line38"></a>var mock;
<a name="line39"></a>
<a name="line40"></a>function setUp() {
<a name="line41"></a>  var obj = new RealObject();
<a name="line42"></a>  mock = new goog.testing.Mock(obj);
<a name="line43"></a>}
<a name="line44"></a>
<a name="line45"></a>function testMockErrorMessage() {
<a name="line46"></a>  var expectation = new goog.testing.MockExpectation(&#39;a&#39;);
<a name="line47"></a>  assertEquals(0, expectation.getErrorMessageCount());
<a name="line48"></a>  assertEquals(&#39;&#39;, expectation.getErrorMessage());
<a name="line49"></a>
<a name="line50"></a>  expectation.addErrorMessage(&#39;foo failed&#39;);
<a name="line51"></a>  assertEquals(1, expectation.getErrorMessageCount());
<a name="line52"></a>  assertEquals(&#39;foo failed&#39;, expectation.getErrorMessage());
<a name="line53"></a>
<a name="line54"></a>  expectation.addErrorMessage(&#39;bar failed&#39;);
<a name="line55"></a>  assertEquals(2, expectation.getErrorMessageCount());
<a name="line56"></a>  assertEquals(&#39;foo failed\nbar failed&#39;, expectation.getErrorMessage());
<a name="line57"></a>}
<a name="line58"></a>
<a name="line59"></a>function testVerifyArgumentList() {
<a name="line60"></a>  var expectation = new goog.testing.MockExpectation(&#39;a&#39;);
<a name="line61"></a>  assertEquals(&#39;&#39;, expectation.getErrorMessage());
<a name="line62"></a>
<a name="line63"></a>  // test single string arg
<a name="line64"></a>  expectation.argumentList = [&#39;foo&#39;];
<a name="line65"></a>  assertTrue(mock.$verifyCall(expectation, &#39;a&#39;, [&#39;foo&#39;]));
<a name="line66"></a>
<a name="line67"></a>  // single numeric arg
<a name="line68"></a>  expectation.argumentList = [2];
<a name="line69"></a>  assertTrue(mock.$verifyCall(expectation, &#39;a&#39;, [2]));
<a name="line70"></a>
<a name="line71"></a>  // single object arg (using standard === comparison)
<a name="line72"></a>  var obj = {prop1: &#39;prop1&#39;, prop2: 2};
<a name="line73"></a>  expectation.argumentList = [obj];
<a name="line74"></a>  assertTrue(mock.$verifyCall(expectation, &#39;a&#39;, [obj]));
<a name="line75"></a>
<a name="line76"></a>  // make sure comparison succeeds if args are similar, but not ===
<a name="line77"></a>  var obj2 = {prop1: &#39;prop1&#39;, prop2: 2};
<a name="line78"></a>  expectation.argumentList = [obj];
<a name="line79"></a>  assertTrue(mock.$verifyCall(expectation, &#39;a&#39;, [obj2]));
<a name="line80"></a>  assertEquals(&#39;&#39;, expectation.getErrorMessage());
<a name="line81"></a>
<a name="line82"></a>  // multiple args
<a name="line83"></a>  expectation.argumentList = [&#39;foo&#39;, 2, obj, obj2];
<a name="line84"></a>  assertTrue(mock.$verifyCall(expectation, &#39;a&#39;, [&#39;foo&#39;, 2, obj, obj2]));
<a name="line85"></a>
<a name="line86"></a>  // test flexible arg matching.
<a name="line87"></a>  expectation.argumentList = [&#39;foo&#39;, matchers.isNumber];
<a name="line88"></a>  assertTrue(mock.$verifyCall(expectation, &#39;a&#39;, [&#39;foo&#39;, 1]));
<a name="line89"></a>
<a name="line90"></a>  expectation.argumentList = [new matchers.InstanceOf(RealObject)];
<a name="line91"></a>  assertTrue(mock.$verifyCall(expectation, &#39;a&#39;, [new RealObject()]));
<a name="line92"></a>}
<a name="line93"></a>
<a name="line94"></a>function testVerifyArgumentListForObjectMethods() {
<a name="line95"></a>  var expectation = new goog.testing.MockExpectation(&#39;toString&#39;);
<a name="line96"></a>  expectation.argumentList = [];
<a name="line97"></a>  assertTrue(mock.$verifyCall(expectation, &#39;toString&#39;, []));
<a name="line98"></a>}
<a name="line99"></a>
<a name="line100"></a>function testRegisterArgumentListVerifier() {
<a name="line101"></a>  var expectationA = new goog.testing.MockExpectation(&#39;a&#39;);
<a name="line102"></a>  var expectationB = new goog.testing.MockExpectation(&#39;b&#39;);
<a name="line103"></a>
<a name="line104"></a>  // Simple matcher that return true if all args are === equivalent.
<a name="line105"></a>  mock.$registerArgumentListVerifier(&#39;a&#39;, function(expectedArgs, args) {
<a name="line106"></a>    return goog.array.equals(expectedArgs, args, function(a, b) {
<a name="line107"></a>      return (a === b);
<a name="line108"></a>    });
<a name="line109"></a>  });
<a name="line110"></a>
<a name="line111"></a>  // test single string arg
<a name="line112"></a>  expectationA.argumentList = [&#39;foo&#39;];
<a name="line113"></a>  assertTrue(mock.$verifyCall(expectationA, &#39;a&#39;, [&#39;foo&#39;]));
<a name="line114"></a>
<a name="line115"></a>  // single numeric arg
<a name="line116"></a>  expectationA.argumentList = [2];
<a name="line117"></a>  assertTrue(mock.$verifyCall(expectationA, &#39;a&#39;, [2]));
<a name="line118"></a>
<a name="line119"></a>  // single object arg (using standard === comparison)
<a name="line120"></a>  var obj = {prop1: &#39;prop1&#39;, prop2: 2};
<a name="line121"></a>  expectationA.argumentList = [obj];
<a name="line122"></a>  expectationB.argumentList = [obj];
<a name="line123"></a>  assertTrue(mock.$verifyCall(expectationA, &#39;a&#39;, [obj]));
<a name="line124"></a>  assertTrue(mock.$verifyCall(expectationB, &#39;b&#39;, [obj]));
<a name="line125"></a>
<a name="line126"></a>  // if args are similar, but not ===, then comparison should succeed
<a name="line127"></a>  // for method with registered object matcher, and fail for method without
<a name="line128"></a>  var obj2 = {prop1: &#39;prop1&#39;, prop2: 2};
<a name="line129"></a>  expectationA.argumentList = [obj];
<a name="line130"></a>  expectationB.argumentList = [obj];
<a name="line131"></a>  assertFalse(mock.$verifyCall(expectationA, &#39;a&#39;, [obj2]));
<a name="line132"></a>  assertTrue(mock.$verifyCall(expectationB, &#39;b&#39;, [obj2]));
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>  // multiple args, should fail for method with registered arg matcher,
<a name="line136"></a>  // and succeed for method without.
<a name="line137"></a>  expectationA.argumentList = [&#39;foo&#39;, 2, obj, obj2];
<a name="line138"></a>  expectationB.argumentList = [&#39;foo&#39;, 2, obj, obj2];
<a name="line139"></a>  assertFalse(mock.$verifyCall(expectationA, &#39;a&#39;, [&#39;foo&#39;, 2, obj2, obj]));
<a name="line140"></a>  assertTrue(mock.$verifyCall(expectationB, &#39;b&#39;, [&#39;foo&#39;, 2, obj2, obj]));
<a name="line141"></a>}
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>function testCreateProxy() {
<a name="line145"></a>  mock = new goog.testing.Mock(RealObject, false, true);
<a name="line146"></a>  assertTrue(mock.$proxy instanceof RealObject);
<a name="line147"></a>  assertThrows(function() {
<a name="line148"></a>    new goog.testing.Mock(RealObject, true, true);
<a name="line149"></a>  });
<a name="line150"></a>  assertThrows(function() {
<a name="line151"></a>    new goog.testing.Mock(1, false, true);
<a name="line152"></a>  });
<a name="line153"></a>}
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>function testValidConstructorArgument() {
<a name="line157"></a>  var someNamespace = { };
<a name="line158"></a>  assertThrows(function() {
<a name="line159"></a>    new goog.testing.Mock(someNamespace.RealObjectWithTypo);
<a name="line160"></a>  });
<a name="line161"></a>}
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>function testArgumentsAsString() {
<a name="line165"></a>  assertEquals(&#39;()&#39;, mock.$argumentsAsString([]));
<a name="line166"></a>  assertEquals(&#39;(string, number, object, null)&#39;,
<a name="line167"></a>               mock.$argumentsAsString([&#39;red&#39;, 1, {}, null]));
<a name="line168"></a>}
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>function testThrowCallExceptionBadArgs() {
<a name="line172"></a>  var msg;
<a name="line173"></a>  mock.$throwException = function(m) {
<a name="line174"></a>    msg = m;
<a name="line175"></a>  };
<a name="line176"></a>
<a name="line177"></a>  mock.$throwCallException(
<a name="line178"></a>      &#39;fn1&#39;, [&#39;b&#39;],
<a name="line179"></a>      { name: &#39;fn1&#39;,
<a name="line180"></a>        argumentList: [&#39;c&#39;],
<a name="line181"></a>        getErrorMessage: function() { return &#39;&#39;; } });
<a name="line182"></a>  assertContains(
<a name="line183"></a>      &#39;Bad arguments to fn1().\nActual: (string)\nExpected: (string)&#39;, msg);
<a name="line184"></a>}
<a name="line185"></a>
<a name="line186"></a>function testThrowCallExceptionUnexpected() {
<a name="line187"></a>  var msg;
<a name="line188"></a>  mock.$throwException = function(m) {
<a name="line189"></a>    msg = m;
<a name="line190"></a>  };
<a name="line191"></a>
<a name="line192"></a>  mock.$throwCallException(&#39;fn1&#39;, [&#39;b&#39;]);
<a name="line193"></a>  assertEquals(&#39;Unexpected call to fn1(string).&#39;, msg);
<a name="line194"></a>}
<a name="line195"></a>
<a name="line196"></a>function testThrowCallExceptionUnexpectedWithNext() {
<a name="line197"></a>  var msg;
<a name="line198"></a>  mock.$throwException = function(m) {
<a name="line199"></a>    msg = m;
<a name="line200"></a>  };
<a name="line201"></a>
<a name="line202"></a>  mock.$throwCallException(
<a name="line203"></a>      &#39;fn1&#39;, [&#39;b&#39;],
<a name="line204"></a>      { name: &#39;fn2&#39;,
<a name="line205"></a>        argumentList: [3],
<a name="line206"></a>        getErrorMessage: function() { return &#39;&#39;; } });
<a name="line207"></a>  assertEquals(
<a name="line208"></a>      &#39;Unexpected call to fn1(string).\n&#39; +
<a name="line209"></a>      &#39;Next expected call was to fn2(number)&#39;, msg);
<a name="line210"></a>}
<a name="line211"></a>
<a name="line212"></a>// This tests that base Object functions which are not enumerable in IE can
<a name="line213"></a>// be mocked correctly.
<a name="line214"></a>function testBindNonEnumerableFunctions() {
<a name="line215"></a>  // Create Foo and override non enumerable functions.
<a name="line216"></a>  var Foo = function() {};
<a name="line217"></a>  Foo.prototype.constructor = function() {
<a name="line218"></a>    fail(&#39;real object should never be called&#39;);
<a name="line219"></a>  };
<a name="line220"></a>  Foo.prototype.hasOwnProperty = function() {
<a name="line221"></a>    fail(&#39;real object should never be called&#39;);
<a name="line222"></a>  };
<a name="line223"></a>  Foo.prototype.isPrototypeOf = function() {
<a name="line224"></a>    fail(&#39;real object should never be called&#39;);
<a name="line225"></a>  };
<a name="line226"></a>  Foo.prototype.propertyIsEnumerable = function() {
<a name="line227"></a>    fail(&#39;real object should never be called&#39;);
<a name="line228"></a>  };
<a name="line229"></a>  Foo.prototype.toLocaleString = function() {
<a name="line230"></a>    fail(&#39;real object should never be called&#39;);
<a name="line231"></a>  };
<a name="line232"></a>  Foo.prototype.toString = function() {
<a name="line233"></a>    fail(&#39;real object should never be called&#39;);
<a name="line234"></a>  };
<a name="line235"></a>  Foo.prototype.valueOf = function() {
<a name="line236"></a>    fail(&#39;real object should never be called&#39;);
<a name="line237"></a>  };
<a name="line238"></a>
<a name="line239"></a>  // Create Mock and set $returns for toString.
<a name="line240"></a>  var mockControl = new goog.testing.MockControl();
<a name="line241"></a>  var mock = mockControl.createLooseMock(Foo);
<a name="line242"></a>  mock.constructor().$returns(&#39;constructor&#39;);
<a name="line243"></a>  mock.hasOwnProperty().$returns(&#39;hasOwnProperty&#39;);
<a name="line244"></a>  mock.isPrototypeOf().$returns(&#39;isPrototypeOf&#39;);
<a name="line245"></a>  mock.propertyIsEnumerable().$returns(&#39;propertyIsEnumerable&#39;);
<a name="line246"></a>  mock.toLocaleString().$returns(&#39;toLocaleString&#39;);
<a name="line247"></a>  mock.toString().$returns(&#39;toString&#39;);
<a name="line248"></a>  mock.valueOf().$returns(&#39;valueOf&#39;);
<a name="line249"></a>
<a name="line250"></a>  // Execute and assert that the Mock is working correctly.
<a name="line251"></a>  mockControl.$replayAll();
<a name="line252"></a>  assertEquals(&#39;constructor&#39;, mock.constructor());
<a name="line253"></a>  assertEquals(&#39;hasOwnProperty&#39;, mock.hasOwnProperty());
<a name="line254"></a>  assertEquals(&#39;isPrototypeOf&#39;, mock.isPrototypeOf());
<a name="line255"></a>  assertEquals(&#39;propertyIsEnumerable&#39;, mock.propertyIsEnumerable());
<a name="line256"></a>  assertEquals(&#39;toLocaleString&#39;, mock.toLocaleString());
<a name="line257"></a>  assertEquals(&#39;toString&#39;, mock.toString());
<a name="line258"></a>  assertEquals(&#39;valueOf&#39;, mock.valueOf());
<a name="line259"></a>  mockControl.$verifyAll();
<a name="line260"></a>}
</pre>


</body>
</html>
